using System.Collections;
using Anvil.Core;

namespace Anvil.Core.Commands
{
	/// <summary>
	/// Execute the data mapping statement
	/// indicated by the QueryID property.
	/// </summary>
	/// <remarks>
	/// If not specified, the QueryID defaults to the command ID.
	/// </remarks>
	public class BaseSelect : BaseMapper
	{
		/// <summary>
		/// Obtain an entity instance
		/// by executing the data mapping statement
		/// indicated by the QueryID property.
		/// </summary>
		/// <remarks><p>
		/// The entity attributes are placed directly 
		/// into the context. 
		/// </p></remarks>
		/// <param name="context">Input/output values</param>
		/// <returns>False; Attributes to context</returns>
		public override bool RequestExecute(IRequestContext context)
		{
			object result = Mapper.QueryForObject(QueryID, context);
			IDictionary row = result as IDictionary;
			if (row != null)
			{
				ICollection keys = row.Keys;
				foreach (string key in keys)
				{
					context[key] = row[key];
				}
			}
			return CONTINUE;
		}
	}
}